package com.jiepos.mpos.core.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.protocol.Protocol;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
 * 
 * <p>标题: —— http工具类</p>
 * <p>描述: —— 简要描述类的职责、实现方式、使用注意事项等</p>
 * <p>模块: 通用平台</p>
 * @author 娄伟峰
 * @version 1.0
 */
public class HttpUtil {
	/** 子类的日志 */
	public final static Logger log = LoggerFactory.getLogger(HttpUtil.class);
	/**
	 * 发送普通http请求，获取响应数据
	 * @param url 请求url
	 * @param data NameValuePair[]数组类型 	格式如下：NameValuePair[] data = { 
				new NameValuePair("user",user),
				new NameValuePair("pass", pass),		 
				new NameValuePair("content",content ) };
	 * @return
	 */
	public static String getHttpClientData(String url,NameValuePair[] data) {
		log.debug("getHttpClient url=="+url);
		HttpClient httpclient = new HttpClient(); 
		 
		// 创建post方法实例
		PostMethod postMethod = new UTF8PostMethod(url );
 
		postMethod.setRequestBody(data);
 		try {
			int statusCode = httpclient.executeMethod(postMethod);
			if (statusCode != HttpStatus.SC_OK) {
				log.debug("Method is wrong url="+url +" error=" + postMethod.getStatusLine());
			}
			InputStream responseBody = postMethod.getResponseBodyAsStream();
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					responseBody, "utf-8"));
			String tmp=null;
			String htmlRet=""; 
			while((tmp=reader.readLine())!=null){
				htmlRet+=tmp+"\n";
			}
			log.info("url="+url+" 接受报文 =" + new String(htmlRet.getBytes()));
			return htmlRet;
		} catch (HttpException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			postMethod.releaseConnection();
		}
		return null;

	}
	
	// Inner class for UTF-8 support
	public static class UTF8PostMethod extends PostMethod {
		public UTF8PostMethod(String url) {
			super(url);
		}

		@Override
		public String getRequestCharSet() {
			// return super.getRequestCharSet();
			return "UTF-8";
		}
	}
	public static String GBKPostMethod(String url,NameValuePair[] data){
		HttpClient httpclient = new HttpClient(); 
		PostMethod postMethod = new GBKPostMethod(url);
		
		postMethod.setRequestBody(data);
 		try {
			int statusCode = httpclient.executeMethod(postMethod);
			if (statusCode != HttpStatus.SC_OK) {
			//	log.debug("Method is wrong uri="+uri +" error=" + postMethod.getStatusLine());
			}
			InputStream responseBody = postMethod.getResponseBodyAsStream();
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					responseBody, "gb2312"));
		/*	String line = reader.readLine();
			return new String(line.getBytes());*/
			String tmp=null;
			String htmlRet=""; 
			while((tmp=reader.readLine())!=null){
				htmlRet+=tmp+"\n";
			}
			log.info("url="+url+" 接受报文 =" + new String(htmlRet.getBytes()));
			return htmlRet;
		} catch (HttpException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			postMethod.releaseConnection();
		}
		return null;
		
	}
	public static class GBKPostMethod extends PostMethod{
		public GBKPostMethod(String url){
			super(url);
		}
		public String getRequestCharSet(){
			return "gb2312";
		}
	}
	
 
	/**
	 * 发送https格式的ssl请求
	 * @param url 请求url地址 
	 * @param data NameValuePair[] 数组格式数据 格式如下：NameValuePair[] data = { 
				new NameValuePair("user",user),
				new NameValuePair("pass", pass),		 
				new NameValuePair("content",content ) };
	 * @return
	 */
	public static String getHttpClientSslData(String url, NameValuePair[] data) {
		log.debug("getHttpClient url=="+url+StringUtils.toString(url));
		Protocol myhttps = new Protocol("https", new MySSLProtocolSocketFactory(), 443);   
		Protocol.registerProtocol("https", myhttps);
		HttpClient httpclient = null;
		try {
			httpclient = new HttpClient();
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} 
		
		// 创建post方法实例
		PostMethod postMethod = new UTF8PostMethod(url );
		if(data != null){
			postMethod.setRequestBody(data);
		}
		
 		try {
			int statusCode = httpclient.executeMethod(postMethod);
			log.debug("响应码statusCode="+statusCode);
			if (statusCode != HttpStatus.SC_OK) {
				log.debug("Method is wrong url="+url +" error=" + postMethod.getStatusLine());
			}
		//	log.debug("str=="+postMethod.getResponseBodyAsString());
			InputStream responseBody = postMethod.getResponseBodyAsStream();
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					responseBody, "utf-8"));
			
	/*		String line = reader.readLine();
			return new String(line.getBytes());*/
			
			
			String tmp=null;
			String htmlRet=""; 
			while((tmp=reader.readLine())!=null){
				htmlRet+=tmp;
			}
			log.info("getHttpClientData url="+url+" 接受报文 =" + new String(htmlRet.getBytes()));
			return htmlRet;
		} catch (HttpException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			postMethod.releaseConnection();
		}
		return null;

	}
	

	 
	/**
	 * 发送ssl请求
	 * @param url
	 * @param uri
	 * @param data
	 * @return
	 */
	public static String getHttpClientSslData2(String url,String uri, String data) {
		log.debug("getHttpClient url=="+url+StringUtils.toString(uri));
		Protocol myhttps = new Protocol("https", new MySSLProtocolSocketFactory(), 443);   
		Protocol.registerProtocol("https", myhttps);
		HttpClient httpclient = null;
		try {
			httpclient = new HttpClient();
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} 
		
		// 创建post方法实例
		PostMethod postMethod = new UTF8PostMethod(url );
		if(data != null){
			postMethod.setRequestBody(data);
		}
		
 		try {
			int statusCode = httpclient.executeMethod(postMethod);
			log.debug("响应码statusCode="+statusCode);
			if (statusCode != HttpStatus.SC_OK) {
				log.debug("Method is wrong uri="+uri +" error=" + postMethod.getStatusLine());
			}
		//	log.debug("str=="+postMethod.getResponseBodyAsString());
			InputStream responseBody = postMethod.getResponseBodyAsStream();
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					responseBody, "utf-8"));
			
	/*		String line = reader.readLine();
			return new String(line.getBytes());*/
			
			
			String tmp=null;
			String htmlRet=""; 
			while((tmp=reader.readLine())!=null){
				htmlRet+=tmp;
			}
			log.info("getHttpClientData url="+url+StringUtils.toString(uri)+" 接受报文 =" + new String(htmlRet.getBytes()));
			return htmlRet;
		} catch (HttpException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			postMethod.releaseConnection();
		}
		return null;

	}
	
	public static String getHttpClientData(String url,String uri, String data) {
		log.debug("getHttpClient uri=="+uri);
		HttpClient httpclient = new HttpClient(); 
		 
		// 创建post方法实例
	 	PostMethod postMethod = new UTF8PostMethod(url );
		postMethod.setRequestBody(data); 
		postMethod.addRequestHeader("Content-Type",
                "application/json; charset=UTF-8");
	 
		   
 		try {
			int statusCode = httpclient.executeMethod(postMethod);
			if (statusCode != HttpStatus.SC_OK) {
				log.debug("Method is wrong uri="+uri +" error=" + postMethod.getStatusLine());
			}
			InputStream responseBody = postMethod.getResponseBodyAsStream();
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					responseBody, "utf-8"));
			String tmp=null;
			String htmlRet=""; 
			while((tmp=reader.readLine())!=null){
				htmlRet+=tmp+"\n";
			}
			log.info("url="+url+" 接受报文 =" + new String(htmlRet.getBytes()));
			return htmlRet;
		} catch (HttpException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			postMethod.releaseConnection();
		}
		return null;

	}

	
}
